package cn.edu.seig.textbook.service.impl;


import cn.edu.seig.textbook.domain.dto.TotalOrders;
import cn.edu.seig.textbook.domain.pojo.Orders;
import cn.edu.seig.textbook.domain.pojo.Textbooks;
import cn.edu.seig.textbook.enums.OrderEnum;
import cn.edu.seig.textbook.mapper.OrdersMapper;
import cn.edu.seig.textbook.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author day
 * @since 2024-11-07
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {
    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private TextbooksServiceImpl textbooksService;

    public List<TotalOrders> getTotalOrders() {
        List<TotalOrders> totalOrdersList = new ArrayList<>();
        Set<Integer> processedTextbookIds = new HashSet<>();  // 用于去重的 Set

        // 假设从数据库中获取数据
        List<Orders> ordersList = ordersMapper.getOrders();

        // 遍历订单，计算每本教材的总订购数量和总价格
        for (Orders order : ordersList) {
            // 检查当前教材是否已经处理过，避免重复添加
            if (processedTextbookIds.contains(order.getTextbookId())) {
                continue;  // 如果已经处理过该教材，跳过
            }

            // 获取教材信息
            Textbooks textbooks = textbooksService.getById(order.getTextbookId());

            TotalOrders totalOrder = new TotalOrders();
            totalOrder.setTextbookId(order.getTextbookId());
            totalOrder.setTextbookName(textbooks.getBookName());
            totalOrder.setAuthor(textbooks.getAuthor());
            totalOrder.setIsbn(textbooks.getISBN());
            totalOrder.setPublishDate(textbooks.getPublishDate());
            totalOrder.setVersion(textbooks.getVersion());
            totalOrder.setPrice(textbooks.getPrice());

            // 将教材添加到 totalOrdersList 中
            totalOrdersList.add(totalOrder);

            // 标记教材已经处理过
            processedTextbookIds.add(order.getTextbookId());
        }

        // 遍历 totalOrdersList 填充购买数量和总价
        for (TotalOrders totalOrder : totalOrdersList) {
            totalOrder.setPurchaseQuantity(ordersMapper.selectpurchase(totalOrder.getTextbookId()));
            totalOrder.setTotalPrice(totalOrder.getPrice() * totalOrder.getPurchaseQuantity());
        }

        return totalOrdersList;
    }

    @Override
    public List<Orders> getOrdersByStudentId(int studentId) {
        return ordersMapper.getOrdersByStudentId(studentId);
    }

    @Override
    public List<Orders> getOrdersByStatus(@Param("orderStatus") OrderEnum orderStatus){
        return ordersMapper.getOrdersByStatus(orderStatus);
    }

    @Override
    public List<Orders> getOrdersByStudentName(@Param("studentName") String studentName){
        return ordersMapper.getOrdersByStudentName(studentName);
    }

    @Override
    public List<Orders> getOrdersByTextbookName(@Param("textbookName") String textbookName){
        return ordersMapper.getOrdersByTextbookName(textbookName);
    }

    @Override
    public int addOrder(Orders order){
        return ordersMapper.addOrder(order);
    }

    @Override
    public int updateOrder(Orders order){
        return ordersMapper.updateOrder(order);
    }

    @Override
    public int deleteOrder(@Param("orderId") int orderId){
        return ordersMapper.deleteOrder(orderId);
    }

    @Override
    public int setOrderStatus(OrderEnum orderStatus) {
        return ordersMapper.setordersStatus(orderStatus);
    }
}
